<template>
    <Form ref="form" :model="form" :rules="ruleValidate" label-position="left" :label-width="120">

        <FormItem label="行政值班项名称" prop="administrativeduty_name.zh">
            <Input v-model="form.administrativeduty_name.zh" placeholder="输入行政值班项名称"></Input>
        </FormItem>

        <FormItem label="排序">
            <InputNumber :min="0" :step="1" v-model="form.sort"></InputNumber>
        </FormItem>

        <FormItem>
            <Button type="primary" :loading="btnload" @click="handleSubmit('form')">{{ condition?'修改':'添加' }}</Button>
        </FormItem>
    </Form>
</template>

<script>
export default {
    props: {
        condition: {
            type: [Boolean, Number],
            default: false
        }
    },
    data () {
        return {
            btnload: false,
            form:{
                administrativeduty_name: {
                    zh: '',
                    mn: '',
                    en: '',
                },
                sort: 0
            },
            addurl: 'api/watchlist/v1/administrativeduty/add',
            editurl: 'api/watchlist/v1/administrativeduty/edit/' + this.condition,
            selUrl: 'api/watchlist/v1/administrativeduty/find/' + this.condition,
            
            ruleValidate: {
                'administrativeduty_name.zh':[
                    { required: true, message: '请输入行政值班项名称', trigger: 'blur' }
                ]
            },
        }
    },
    methods: {
        
        /************************************************************
         * 
         * 表单验证
         * @param   {name}            要验证的表单
         * 
         ************************************************************/
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.btnload = true
                    if (this.condition) {
                        this.editSubmit ()
                    } else {
                        this.addSubmit ()
                    }
                } else {
                    this.$Message.error('请检查填写内容');
                }
            })
        },
        
        /************************************************************
         * 
         * 执行添加数据
         * 
         ************************************************************/
        addSubmit () {
            this.$http.post(this.addurl, this.form).then(val=>{
                this.btnload = false
                this.$emit('requery',true)
            }).catch(err => {
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 
         * 执行修改数据
         * 
         ************************************************************/
        editSubmit () {
            this.$http.put(this.editurl, this.form).then(val=>{
                this.btnload = false
                this.$emit('requery',true)
            }).catch(err => {
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 
         * 单条数据查询（修改）
         * 
         ************************************************************/
        selData () {
            this.$http.get(this.selUrl).then(val=>{
                let data = val.data
                this.form.administrativeduty_name = data.administrativeduty_name
                this.form.sort = data.sort
            }).catch(err => {
            })
        }
    },
    created () {
        if (this.condition) this.selData ()
    },
}
</script>